#include <gmlib/hash_lib/hmac.h>
#include <gmlib/sm3/sm3.h>

#include <cstring>
#include <stdexcept>

#include "test.h"

using namespace sm3;

using SM3HMac = hash_lib::HMac<SM3>;

static std::uint8_t msg[64] = {0};
static std::uint8_t out[SM3::DIGEST_SIZE];

static std::uint8_t key_1[16]                  = {0};
static std::uint8_t digest_1[SM3::DIGEST_SIZE] = {
    0xAB, 0xBC, 0xED, 0x41, 0x5D, 0xF5, 0xCA, 0x97, //
    0x4A, 0x87, 0xAF, 0xCC, 0x7D, 0xA6, 0x87, 0x57, //
    0x18, 0xCB, 0x2C, 0x70, 0x3D, 0x61, 0xA1, 0x8C, //
    0x22, 0x69, 0x22, 0x98, 0xE7, 0xAF, 0xAF, 0x79, //
};

static std::uint8_t key_2[1024] = {
    0x35, 0x86, 0x95, 0x33, 0xc0, 0xee, 0x33, 0x59, 0x09, 0x07, 0xbe, 0x0a,
    0xe7, 0x68, 0xe4, 0x42, 0x88, 0x15, 0x5d, 0x0f, 0x3d, 0xd4, 0x8b, 0xd2,
    0x51, 0x49, 0x9d, 0x4a, 0x63, 0x7c, 0x18, 0xb5, 0x14, 0x62, 0x16, 0xb9,
    0x39, 0x0b, 0xc9, 0x96, 0x9e, 0x2e, 0xd7, 0x87, 0xfd, 0x47, 0x73, 0x33,
    0x6f, 0x27, 0xd1, 0x06, 0x6a, 0xe5, 0x81, 0x52, 0xc3, 0x97, 0xa1, 0x73,
    0x6d, 0xba, 0x6d, 0x8d, 0x92, 0x90, 0xb9, 0x67, 0x46, 0xe3, 0x76, 0x9d,
    0xbe, 0xd3, 0xf2, 0xa5, 0x6c, 0x1b, 0x94, 0x1d, 0x1c, 0x88, 0x6d, 0x95,
    0x08, 0x50, 0x25, 0xa1, 0x64, 0xb9, 0x0e, 0xd7, 0xfb, 0x34, 0x06, 0x5b,
    0x65, 0xb2, 0xea, 0x81, 0xb8, 0x80, 0x2e, 0x7f, 0xf5, 0x56, 0xbf, 0x09,
    0x10, 0x37, 0xc5, 0xee, 0x75, 0x38, 0x11, 0x00, 0x0d, 0x03, 0xfe, 0x1f,
    0xed, 0x2b, 0x25, 0xd2, 0x83, 0x0d, 0x8b, 0xac, 0xf0, 0x6e, 0xee, 0x19,
    0xcc, 0xc9, 0x51, 0x2c, 0x26, 0xb5, 0x98, 0x35, 0x3a, 0xe9, 0xd2, 0x37,
    0x69, 0x83, 0xb1, 0xf4, 0x59, 0xcc, 0xa4, 0x12, 0x47, 0x84, 0x06, 0x52,
    0x95, 0x90, 0xa4, 0xdf, 0x7e, 0x0a, 0xbb, 0xbd, 0x2b, 0x59, 0xc8, 0xd1,
    0x26, 0xca, 0xf9, 0x0b, 0x11, 0x7d, 0xab, 0x4c, 0xf0, 0x54, 0xc3, 0x1b,
    0x8a, 0x19, 0x32, 0x4d, 0x6b, 0x92, 0x83, 0x18, 0xc8, 0xd1, 0x5b, 0xb2,
    0x41, 0xb4, 0xc2, 0xe4, 0xe0, 0xa8, 0x4d, 0xe6, 0xce, 0xfc, 0x70, 0x15,
    0x8d, 0x4a, 0xcc, 0xa6, 0x2b, 0x61, 0x67, 0x4c, 0x71, 0xbb, 0x64, 0xf9,
    0xe3, 0x99, 0xd8, 0x25, 0x6e, 0x53, 0x71, 0xc9, 0xf9, 0x3d, 0xac, 0xb2,
    0x9b, 0xb3, 0x93, 0xb7, 0x5e, 0x85, 0x8d, 0x36, 0x8f, 0x12, 0x77, 0x71,
    0x00, 0xcf, 0xa4, 0x2f, 0x6c, 0x55, 0x5a, 0x98, 0x4b, 0xee, 0xe8, 0x6d,
    0xe6, 0x8c, 0xa0, 0x6e, 0xf3, 0xfe, 0xce, 0xac, 0x26, 0x87, 0x24, 0x5b,
    0x17, 0xbd, 0x44, 0xd3, 0x65, 0xc0, 0xe3, 0x39, 0x7b, 0x36, 0x6d, 0x8e,
    0x2a, 0xb1, 0xa3, 0xea, 0x9a, 0x07, 0xfa, 0x82, 0x4d, 0x68, 0x56, 0x27,
    0xf5, 0x09, 0x65, 0x7e, 0xcf, 0xb3, 0xae, 0x41, 0xb6, 0x66, 0x7e, 0xa7,
    0x8a, 0x1b, 0x05, 0xf6, 0xf3, 0x32, 0x49, 0xb9, 0x74, 0x32, 0xca, 0x0d,
    0x2d, 0xd4, 0xdc, 0x66, 0xf1, 0xd9, 0xa8, 0xea, 0x01, 0xcb, 0xe0, 0xc8,
    0xe1, 0x31, 0x99, 0x7d, 0xc2, 0x86, 0xfd, 0xe3, 0xb0, 0x93, 0x66, 0x25,
    0x57, 0xc5, 0x61, 0xd9, 0x7d, 0x50, 0xf3, 0x7c, 0xca, 0x80, 0x53, 0x53,
    0x2d, 0x73, 0x46, 0xb1, 0xc1, 0xef, 0x85, 0x06, 0x62, 0x63, 0x20, 0x42,
    0x92, 0xa9, 0xe6, 0x8a, 0xe9, 0xb2, 0x20, 0x1e, 0x99, 0x61, 0x54, 0x13,
    0xd1, 0xbc, 0xc7, 0xef, 0x4d, 0x0e, 0xb4, 0x03, 0x02, 0x4f, 0xc8, 0xd9,
    0xda, 0x0f, 0xba, 0xd3, 0xa8, 0x7e, 0xb0, 0x4c, 0x20, 0xce, 0xbe, 0xe5,
    0x93, 0xe3, 0x6e, 0x29, 0x82, 0x3b, 0xd5, 0xf0, 0x2a, 0xf2, 0x40, 0x4b,
    0x54, 0x38, 0xe8, 0x1f, 0x50, 0x7f, 0xeb, 0x01, 0x51, 0x43, 0xa5, 0x1c,
    0x7b, 0xd4, 0xf6, 0x70, 0xa8, 0x76, 0x77, 0x09, 0x80, 0x90, 0x3f, 0xbd,
    0x4a, 0xe0, 0x36, 0x54, 0x1f, 0x67, 0xdf, 0xd6, 0x12, 0xff, 0xe4, 0x91,
    0xbd, 0x51, 0xd8, 0x43, 0xa8, 0xab, 0xf9, 0x58, 0x0e, 0x04, 0x9a, 0x41,
    0x89, 0x45, 0xbf, 0x49, 0x36, 0x65, 0xcd, 0x5e, 0x2a, 0xce, 0xdd, 0xe3,
    0x7c, 0x5a, 0xde, 0x68, 0xca, 0x13, 0x8a, 0x22, 0x6e, 0xcb, 0x95, 0xe6,
    0xd6, 0xba, 0x73, 0x85, 0x37, 0x41, 0x27, 0xb7, 0x32, 0xbd, 0xc4, 0xd1,
    0xf2, 0xca, 0xd0, 0x63, 0xec, 0x35, 0x09, 0x49, 0x79, 0x2b, 0x99, 0x2c,
    0x9b, 0xbb, 0xee, 0xe1, 0x8e, 0x2c, 0x50, 0x1a, 0x82, 0x39, 0x45, 0xfa,
    0x36, 0xce, 0x9b, 0x86, 0x89, 0x49, 0x4e, 0xe3, 0x35, 0x2a, 0x63, 0x44,
    0xbf, 0xca, 0x07, 0xa4, 0xe0, 0x25, 0x62, 0x21, 0x06, 0xc0, 0x04, 0x2d,
    0x74, 0x5a, 0xf4, 0x58, 0xf4, 0x68, 0xc4, 0x9c, 0x14, 0x99, 0xfb, 0x54,
    0xf9, 0xca, 0xa4, 0xf1, 0x80, 0xd2, 0x83, 0x00, 0x16, 0xa5, 0xd8, 0x39,
    0x00, 0x12, 0x56, 0x5f, 0x9a, 0x14, 0xf8, 0x69, 0x1c, 0x14, 0x03, 0x09,
    0x51, 0xab, 0x7d, 0x05, 0x87, 0xda, 0xba, 0x35, 0x96, 0x9f, 0x91, 0xa3,
    0xba, 0x6c, 0x50, 0xff, 0x80, 0x0b, 0x98, 0xc4, 0xe0, 0x23, 0x0e, 0x46,
    0x26, 0x32, 0x5b, 0x1b, 0x5d, 0x0b, 0xa5, 0x21, 0x29, 0x7e, 0xa9, 0xa1,
    0xac, 0x78, 0x2d, 0x5d, 0xd5, 0xd3, 0xdb, 0xc2, 0xf2, 0x55, 0xa3, 0xcb,
    0x72, 0xd4, 0xa2, 0xe9, 0xa5, 0x3a, 0x17, 0x56, 0xa6, 0x6d, 0xef, 0xb5,
    0xd3, 0xa3, 0x61, 0x39, 0x4c, 0x01, 0xc0, 0x0d, 0x75, 0x89, 0xc7, 0x8d,
    0x99, 0x80, 0x6a, 0x77, 0x0b, 0xaf, 0x52, 0x14, 0xcb, 0x49, 0x67, 0x5f,
    0x3a, 0x2e, 0x10, 0x7b, 0xa4, 0x48, 0x94, 0x37, 0xc6, 0x82, 0xff, 0xc1,
    0x52, 0x71, 0x1f, 0x38, 0xd5, 0x12, 0x2d, 0x44, 0xdf, 0xb3, 0x9b, 0x3e,
    0x93, 0x99, 0x73, 0x34, 0x23, 0x73, 0x0b, 0xc1, 0x1b, 0xe5, 0x00, 0x6d,
    0xd2, 0xba, 0xd1, 0xc6, 0x9a, 0x26, 0x6c, 0x69, 0xcf, 0x8b, 0xdb, 0x00,
    0x4f, 0xdc, 0x6a, 0xad, 0x94, 0xd6, 0xb9, 0x08, 0xe6, 0xeb, 0x01, 0x54,
    0x8e, 0xc0, 0x16, 0x9b, 0x6e, 0x60, 0xe6, 0x44, 0x4e, 0x96, 0xc4, 0x17,
    0xfc, 0xe9, 0xba, 0xa7, 0x92, 0x37, 0xff, 0x7e, 0x1a, 0x1d, 0xa8, 0x5a,
    0x8e, 0x57, 0x7f, 0xa2, 0xa1, 0xc2, 0xca, 0xfe, 0x6d, 0xcc, 0xf8, 0xab,
    0x29, 0xf8, 0xc3, 0x05, 0xcd, 0x93, 0xa9, 0xd5, 0xb7, 0x39, 0x23, 0xef,
    0xe6, 0x60, 0xac, 0x7b, 0xb4, 0xaa, 0xd9, 0x92, 0x98, 0xc2, 0xdd, 0x0e,
    0xf2, 0x45, 0xcb, 0x27, 0x01, 0x3b, 0x6b, 0x83, 0x01, 0xb7, 0x9f, 0x77,
    0xf1, 0x45, 0x7c, 0x2e, 0x81, 0xe1, 0xa9, 0x8a, 0x8f, 0x4f, 0xbf, 0x78,
    0x4a, 0x10, 0x6b, 0x96, 0xb0, 0xd0, 0x45, 0x58, 0x39, 0x2b, 0xba, 0x2e,
    0x02, 0xc1, 0xc1, 0x36, 0x9b, 0x13, 0xf0, 0xc8, 0xe2, 0xfe, 0x65, 0x12,
    0x88, 0x18, 0xbb, 0xfb, 0x64, 0x37, 0x61, 0xc0, 0x49, 0x23, 0x3b, 0x1e,
    0xb6, 0x57, 0x6a, 0x5b, 0x27, 0xaa, 0x8c, 0xf9, 0xf5, 0x91, 0xd9, 0xba,
    0x1e, 0x45, 0x9a, 0x64, 0xb3, 0x9f, 0xeb, 0x15, 0xd1, 0xbb, 0x9f, 0xae,
    0x6f, 0xa7, 0xa2, 0x09, 0xc6, 0x1b, 0xd2, 0x8f, 0xd9, 0x90, 0x55, 0x14,
    0x44, 0x07, 0x66, 0x85, 0xb3, 0xc4, 0xd5, 0xcc, 0x5c, 0xd6, 0x3a, 0xa9,
    0x61, 0x94, 0xc8, 0x62, 0x4e, 0x9f, 0xad, 0x81, 0xb0, 0x84, 0xd4, 0x4b,
    0xd8, 0xc9, 0x22, 0xc5, 0x94, 0x9c, 0x6c, 0x0d, 0x25, 0xd0, 0x15, 0x74,
    0x8c, 0x77, 0x29, 0x58, 0x2a, 0xb5, 0x98, 0x24, 0x2b, 0x6c, 0x44, 0x55,
    0xb4, 0x70, 0xcf, 0xa1, 0xd4, 0x56, 0xfc, 0xc9, 0xa0, 0xd4, 0x8f, 0x6a,
    0xd0, 0x3d, 0xd3, 0x37, 0xc0, 0x62, 0xb5, 0x8f, 0x58, 0x22, 0x7f, 0x22,
    0x95, 0x82, 0xfd, 0x97, 0x56, 0xc3, 0xb0, 0xd1, 0xd5, 0x51, 0x51, 0xa0,
    0xe2, 0xc4, 0xf4, 0xc3, 0xe1, 0x26, 0x4b, 0x9a, 0x3e, 0xbe, 0xeb, 0x89,
    0xc6, 0x7c, 0x40, 0xf3, 0x09, 0x5f, 0x42, 0xc0, 0xec, 0x9e, 0xa5, 0xb6,
    0x34, 0x2f, 0x7d, 0x26, 0x74, 0x96, 0xa2, 0xcb, 0xff, 0x79, 0x30, 0x2c,
    0xf8, 0x8d, 0x26, 0x71, 0x57, 0x13, 0x77, 0xaa, 0x61, 0x66, 0x2a, 0x58,
    0x49, 0x51, 0x63, 0xae, 0xcc, 0x72, 0x66, 0xe0, 0xfd, 0xf1, 0x77, 0xfe,
    0xe5, 0x50, 0x6e, 0x60,
};
static std::uint8_t msg_2[512] = {
    0xe6, 0x83, 0x70, 0xcf, 0x76, 0x34, 0x88, 0xca, 0xba, 0xc0, 0x8d, 0x8b,
    0x44, 0xf6, 0x94, 0x43, 0x75, 0x12, 0x98, 0xae, 0x16, 0x27, 0xc2, 0x24,
    0x60, 0xc0, 0x60, 0x41, 0xcd, 0x21, 0xe9, 0xb7, 0xfc, 0x5e, 0x3e, 0xde,
    0xff, 0x6f, 0x4d, 0x70, 0x1e, 0x93, 0xc1, 0x6e, 0xb1, 0x5d, 0x06, 0x37,
    0xec, 0x40, 0xee, 0xab, 0xb1, 0xb2, 0x1c, 0x94, 0x30, 0x5a, 0xb4, 0xb2,
    0x82, 0x0b, 0xdd, 0x27, 0x2f, 0xc8, 0x0c, 0x86, 0xa7, 0x59, 0x2c, 0xf9,
    0x5e, 0xb7, 0xad, 0x1c, 0x4d, 0x0d, 0x94, 0x1f, 0xd6, 0xc3, 0xd5, 0x44,
    0xb8, 0x48, 0x41, 0x14, 0x08, 0x11, 0x15, 0x0d, 0x29, 0x76, 0x96, 0x2f,
    0x0a, 0x53, 0xab, 0x6b, 0x56, 0xcd, 0xf8, 0x41, 0xff, 0x00, 0x08, 0xa3,
    0xa4, 0x28, 0xb0, 0x84, 0x09, 0x22, 0x47, 0x8d, 0xa7, 0xa6, 0x47, 0x84,
    0xee, 0x5b, 0x4e, 0x83, 0xb1, 0xdb, 0x04, 0x23, 0x3d, 0x3f, 0xc3, 0x27,
    0xb6, 0x7b, 0xf7, 0x53, 0xce, 0x41, 0x28, 0xd3, 0x8c, 0x83, 0xc7, 0x42,
    0xb7, 0x3e, 0xa9, 0xd1, 0x42, 0xa3, 0xea, 0x79, 0x44, 0x03, 0x80, 0x88,
    0xdc, 0xaa, 0x1c, 0x9d, 0x9e, 0xf0, 0x46, 0xc6, 0x2e, 0x5b, 0x39, 0x41,
    0x0e, 0x66, 0xc5, 0x64, 0xdc, 0x7c, 0xe1, 0x90, 0x87, 0x99, 0x85, 0x54,
    0xc3, 0x85, 0x23, 0x8a, 0x4f, 0x48, 0xa5, 0xd5, 0x17, 0xe2, 0xc8, 0xef,
    0x18, 0x36, 0x1b, 0x4b, 0x37, 0x0c, 0x9b, 0x19, 0x1f, 0xc6, 0x5c, 0xd0,
    0xea, 0x84, 0x4e, 0x58, 0x95, 0x2e, 0x8a, 0x83, 0x40, 0xe7, 0x48, 0x64,
    0xa7, 0x6e, 0xd4, 0xb4, 0xba, 0xe2, 0x08, 0xa2, 0xb9, 0xb1, 0xb4, 0x49,
    0xcb, 0x3c, 0xff, 0xf4, 0x0b, 0xea, 0x7e, 0xea, 0xc4, 0x37, 0x82, 0xe4,
    0x65, 0x7a, 0x71, 0xb8, 0x64, 0x00, 0x09, 0x64, 0x71, 0x82, 0x94, 0x1b,
    0x5b, 0x5e, 0x2e, 0x35, 0x3c, 0x15, 0x1b, 0xb4, 0x49, 0x16, 0xd4, 0x73,
    0x5b, 0x7c, 0xdf, 0x87, 0xb3, 0x60, 0xe4, 0x14, 0xc9, 0x76, 0x4a, 0x42,
    0xa7, 0xcd, 0xd0, 0xa4, 0xea, 0xd1, 0x31, 0x2a, 0xcc, 0x73, 0x65, 0x42,
    0x2a, 0x36, 0x57, 0x53, 0x84, 0x67, 0x86, 0x97, 0xb2, 0xce, 0xf6, 0xff,
    0xe0, 0x5d, 0xdc, 0x3e, 0x79, 0x79, 0xe9, 0x32, 0xbb, 0x90, 0xbd, 0x31,
    0x16, 0xf0, 0xe3, 0xd2, 0x0f, 0xcd, 0xe1, 0x9c, 0xe9, 0x75, 0xce, 0x44,
    0x6c, 0x96, 0xd0, 0xdc, 0xd8, 0xe8, 0xc3, 0x7a, 0xd9, 0xeb, 0xda, 0x6c,
    0x3b, 0x54, 0x8d, 0x6a, 0xbd, 0x59, 0x0d, 0x52, 0x59, 0x53, 0x6a, 0xd9,
    0x39, 0xc7, 0xaf, 0x42, 0x64, 0xf0, 0xb2, 0x05, 0x81, 0x39, 0x2b, 0x2c,
    0xcc, 0x84, 0x6b, 0x42, 0x88, 0x58, 0xec, 0x12, 0xfd, 0x68, 0x69, 0x36,
    0xd5, 0x86, 0xb3, 0x2f, 0xfc, 0xa2, 0xf6, 0xce, 0x7c, 0x67, 0x52, 0xc8,
    0x7d, 0xa1, 0x15, 0x09, 0x00, 0xb2, 0xed, 0x10, 0xb3, 0x97, 0x87, 0xf7,
    0xcc, 0x7a, 0x40, 0xf9, 0x31, 0x9e, 0xfc, 0xa4, 0x24, 0x9f, 0xd7, 0xfe,
    0x79, 0xbd, 0x74, 0x63, 0x02, 0x6f, 0xc1, 0x5c, 0xac, 0x77, 0x2c, 0x81,
    0x6d, 0x91, 0xde, 0xc1, 0xb9, 0xa1, 0x73, 0x9e, 0xb3, 0xb0, 0xc1, 0xb7,
    0x2d, 0xf5, 0x91, 0x92, 0x1c, 0x3e, 0x83, 0x49, 0x76, 0x8e, 0x7b, 0xd4,
    0x23, 0x2b, 0x5b, 0xb7, 0xd6, 0x89, 0x2d, 0x4a, 0x47, 0xe4, 0x6d, 0xb0,
    0x5b, 0x49, 0x32, 0xc0, 0xcc, 0x93, 0x11, 0xc2, 0xfb, 0x2e, 0x6f, 0xa9,
    0xc0, 0x20, 0x7c, 0x2c, 0x44, 0x46, 0xa0, 0xf2, 0xf5, 0xad, 0x83, 0x23,
    0x4d, 0x6a, 0x4b, 0x59, 0xd7, 0x99, 0x00, 0x1e, 0x2a, 0x32, 0xd8, 0x3f,
    0xda, 0xae, 0x9f, 0x6f, 0x55, 0xed, 0x69, 0x21, 0x17, 0xe9, 0xa6, 0x21,
    0xc4, 0xf4, 0x7a, 0x1b, 0x1a, 0x36, 0x4a, 0xf3,
};
static std::uint8_t digest_2[32] = {
    0xc0, 0xd0, 0x34, 0x38, 0x69, 0xf3, 0x14, 0x75, 0xf4, 0x7b, 0x0c,
    0x44, 0x75, 0x85, 0x3b, 0xa8, 0xc6, 0xc6, 0x43, 0xa7, 0x2e, 0xc5,
    0xaa, 0xaf, 0x21, 0xda, 0x92, 0x7d, 0x06, 0x2d, 0x61, 0x41,
};

static std::uint8_t key_3[SM3::BLOCK_SIZE]     = {0};
static std::uint8_t digest_3[SM3::DIGEST_SIZE] = {
    0xAB, 0xBC, 0xED, 0x41, 0x5D, 0xF5, 0xCA, 0x97, //
    0x4A, 0x87, 0xAF, 0xCC, 0x7D, 0xA6, 0x87, 0x57, //
    0x18, 0xCB, 0x2C, 0x70, 0x3D, 0x61, 0xA1, 0x8C, //
    0x22, 0x69, 0x22, 0x98, 0xE7, 0xAF, 0xAF, 0x79, //
};

void test_hmac()
{
    std::uint8_t out[SM3HMac::DIGEST_SIZE];
    auto         mac = SM3HMac();
    mac.set_key(key_1, sizeof(key_1));
    mac.do_final(out, msg, sizeof(msg));
    if (std::memcmp(out, digest_1, SM3HMac::DIGEST_SIZE) != 0)
    {
        throw std::runtime_error("err in hmac");
    }
    mac.set_key(key_2, sizeof(key_2));
    mac.do_final(out, msg_2, sizeof(msg_2));
    if (std::memcmp(out, digest_2, SM3HMac::DIGEST_SIZE) != 0)
    {
        throw std::runtime_error("err in hmac");
    }
    mac.set_key(key_3, sizeof(key_3));
    mac.do_final(out, msg, sizeof(msg));
    if (std::memcmp(out, digest_3, SM3HMac::DIGEST_SIZE) != 0)
    {
        throw std::runtime_error("err in hmac");
    }
}